
"""
Module that displays the normal view of the world as a pygame Surface
"""

import pygame
import util
from world import world, town

WORLD_SIZE = (600, 600)
TOWN_SIZE = (90, 90)
TEXT_SIZE = (25, 25)
TOWN_SPACER = 3
ROUTE_SIZE = (20, 20)
BUFFER = (20, 20)
ROUTE_SPECTRUM = util.ColorSpectrum()
MERCHANT_COLOR = (255, 0 , 0)
MERCHANT_RADIUS = 8

class WorldDisplay(pygame.Surface):
    """A surface that displays a visual representation of the world"""
    
    def __init__(self, w):
        pygame.Surface.__init__(self, WORLD_SIZE)
        self.fill((150, 150, 150))
        self.set_world(w)
        
    def set_world(self, w):
        """
        Sets the world that this display should show.
        """
        self.towns = []
        self.south_routes = []
        self.east_routes = []
        for i in range(w.get_width()):
            town_column = []
            s_column = []
            e_column = []
            for j in range(w.get_height()):
                town_column.append(TownDisplay(w.get_town(i, j)))
                if i > 0:
                    e_column.append(RouteDisplay(w.get_route(i-1, j, i, j)))
                if j > 0:
                    s_column.append(RouteDisplay(w.get_route(i, j-1, i, j)))
            
            self.towns.append(town_column)
            self.south_routes.append(s_column)
            if i > 0:self.east_routes.append(e_column)
        
            
        self.update()
                
    
    def update(self):
        #draw towns
        x = BUFFER[0]
        for column in self.towns:
            y = BUFFER[1]
            for town in column:
                town.update()
                self.blit(town, (x, y))
                y = y + TOWN_SIZE[1] + ROUTE_SIZE[1]
            x = x + TOWN_SIZE[0] + ROUTE_SIZE[0]
            
        #draw east routes
        x = BUFFER[0] + TOWN_SIZE[0]
        for column in self.east_routes:
            y = BUFFER[1] + (TOWN_SIZE[1] - ROUTE_SIZE[1]) / 2
            for route in column:
                route.update()
                self.blit(route, (x, y))
                y = y + ROUTE_SIZE[1] + TOWN_SIZE[1]
            x = x + ROUTE_SIZE[0] + TOWN_SIZE[0]
        
        #draw south routes
        x = BUFFER[0] + (TOWN_SIZE[0] - ROUTE_SIZE[0]) / 2
        for column in self.south_routes:
            y = BUFFER[1] + TOWN_SIZE[1]
            for route in column:
                route.update()
                self.blit(route, (x, y))
                y = y + ROUTE_SIZE[1] + TOWN_SIZE[1]
            x = x + ROUTE_SIZE[0] + TOWN_SIZE[0]
            

class TownDisplay(pygame.Surface):
    
    def __init__(self, town):
        pygame.Surface.__init__(self, TOWN_SIZE)
        self.house, rect  = util.load_image("house.bmp")
        self.house_w = rect.w
        self.house_h = rect.h
        self._town = town
        self.update()
        
        
        
    def update(self):
        
        x = TOWN_SIZE[0] / 2 - self.house_w / 2
        y = 0
        self.fill((0, 0, 0))
        self.blit(self.house, (x, y))
        text_spacer = TEXT_SIZE[0]/ (len(town.ALL_GOODS) + 1)
        x = 0
        y = self.house_h + TOWN_SPACER
        font = pygame.font.Font(None, TEXT_SIZE[1])
        for good in town.ALL_GOODS:
            
            x = x + text_spacer
            text = font.render(str(self._town.prices[good]), 1, (255, 255, 255))
            self.blit(text, (x, y))
            x = x + TEXT_SIZE[0]
        if self._town.merchants:
            x = TOWN_SIZE[0] / 2
            y = y + TOWN_SPACER + MERCHANT_RADIUS + TEXT_SIZE[1]
            pygame.draw.circle(self, MERCHANT_COLOR, (x, y), MERCHANT_RADIUS)
        
class RouteDisplay(pygame.Surface):
    
    def __init__(self, route):     
        pygame.Surface.__init__(self, ROUTE_SIZE)
        self.update(route)
        
        
    def update(self, route = None):
        if route: self.route = route
        if not self.route:
            raise Exception("RouteDisplay updated with no route")
        cost = self.route.get_cost()
        self.fill(ROUTE_SPECTRUM.get_color(cost))
        
        
    